package com.scala.actor

import scala.actors.Actor

class AppleActor extends Actor {

  def act(): Unit = {
    while (true) {
      receive {
        case "start" => {
          println("starting ...")
        }
        case SyncMsg(id, msg) => {
          println(id + ",sync " + msg)
          Thread.sleep(3000)
          sender ! ReplyMsg(3, "finished")
        }
        case AsyncMsg(id, msg) => {
          println("AsyncMsg: " + id + " sync2 " + msg)
          println()
          Thread.sleep(3000)
        }
      }
    }
  }
}

object AppleActor {
  def main(args: Array[String]) {
    val apple = new AppleActor
    apple.start()

    //异步消息
    apple ! AsyncMsg(1, "hello actor")
    println("异步消息发送完成")

    //同步消息
    val content = apple !? (3000, SyncMsg(2, "hello actor"))
    println("content: " + content)
    println("同步消息发送完成\n")

    val reply = apple !! SyncMsg(2, "hello actor")
    val c = reply.apply()
    println(reply.isSet)
    println(c)
  }
}

case class SyncMsg(id: Int, msg: String)

case class AsyncMsg(id: Int, msg: String)

case class ReplyMsg(id: Int, msg: String)
